package org.jpro.modules.system.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jpro.common.web.BaseController;
import org.jpro.modules.system.entity.Permission;
import org.jpro.modules.system.service.IPermissionService;
import org.jpro.modules.system.service.IRolePermissionService;
import org.jpro.modules.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 权限controller
 */
@Controller
@RequestMapping("/system/permission")
public class PermissionController extends BaseController {

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private IRolePermissionService rolePermissionService;

    /**
     * 默认页面
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list() {
        return "system/permissionList";
    }

    /**
     * 菜单页面
     */
    @RequestMapping(value = "menu", method = RequestMethod.GET)
    public String menuList() {
        return "system/menuList";
    }

    /**
     * 菜单集合(JSON)
     */
    @ResponseBody
    @RequiresPermissions("sys:perm:menu:view")
    @RequestMapping(value = "menu/json", method = RequestMethod.GET)
    public List<Permission> menuDate() {
        return permissionService.getMenus();
    }

    /**
     * 权限集合(JSON)
     */
    @ResponseBody
    @RequiresPermissions("sys:perm:view")
    @RequestMapping(value = "json", method = RequestMethod.GET)
    public List<Permission> getData() {
        return permissionService.getAll();
    }

    /**
     * 获取菜单下的操作
     */
    @ResponseBody
    @RequestMapping("ope/json")
    @RequiresPermissions("sys:perm:view")
    public Map<String, Object> menuOperationDate(Integer pid) {
        List<Permission> menuOperList = permissionService.getMenuOperation(pid);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", menuOperList);
        map.put("total", menuOperList.size());
        return map;
    }

    /**
     * 当前登录用户的权限集合
     */
    @ResponseBody
    @RequestMapping("i/json")
    public List<Permission> myPermissionDate() {
        return permissionService.getPermissions(UserUtil.getCurrentUser().getId());
    }

    /**
     * 某用户的权限集合
     */
    @ResponseBody
    @RequestMapping("{userId}/json")
    @RequiresPermissions("sys:perm:view")
    public List<Permission> otherPermissionDate(@PathVariable("userId") Integer userId) {
        List<Permission> permissionList = permissionService.getPermissions(userId);
        return permissionList;
    }

    /**
     * 添加权限跳转
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Map model) {
        model.put("permission", new Permission());
        model.put("action", "create");
        return "system/permissionForm";
    }

    /**
     * 添加菜单跳转
     */
    @RequestMapping(value = "menu/create", method = RequestMethod.GET)
    public String menuCreateForm(Map model) {
        model.put("permission", new Permission());
        model.put("action", "create");
        return "system/menuForm";
    }

    /**
     * 添加权限/菜单
     */
    @ResponseBody
    @RequiresPermissions("sys:perm:add")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public void create(@Valid Permission permission) {
        permissionService.save(permission);
    }

    /**
     * 添加菜单基础操作
     * @param pid
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sys:perm:add")
    @RequestMapping(value = "createBase/{pname}/{pid}", method = RequestMethod.GET)
    public void create(@PathVariable("pname") String pname, @PathVariable("pid") Integer pid) {
        permissionService.addBaseOpe(pid, pname);
    }

    /**
     * 修改权限跳转
     */
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Integer id, Map model) {
        model.put("permission", permissionService.get(id));
        model.put("action", "update");
        return "system/permissionForm";
    }

    /**
     * 修改菜单跳转
     */
    @RequestMapping(value = "menu/update/{id}", method = RequestMethod.GET)
    public String updateMenuForm(@PathVariable("id") Integer id, Map model) {
        model.put("permission", permissionService.get(id));
        model.put("action", "update");
        return "system/menuForm";
    }

    /**
     * 修改权限/菜单
     */
    @ResponseBody
    @RequiresPermissions("sys:perm:update")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public void update(@Valid @ModelAttribute("permission") Permission permission) {
        permissionService.save(permission);
    }

    /**
     * 删除权限
     */
    @ResponseBody
    @RequiresPermissions("sys:perm:delete")
    @RequestMapping(value = "delete/{id}")
    public void delete(@PathVariable("id") Integer id) {
        permissionService.delete(id);
    }

    @ModelAttribute
    public void getPermission(@RequestParam(value = "id", defaultValue = "-1") Integer id, Map model) {
        if (id != -1) {
            model.put("permission", permissionService.get(id));
        }
    }
}
